package id.co.rho.scoring.cid.client.mvc.typemodelconfig;

import id.co.rho.scoring.cid.client.command.CommandDispatcherAsync;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigCommand.CommandType;
import id.co.rho.scoring.cid.client.command.impl.TypeModelConfigResult;
import id.co.rho.scoring.cid.client.mvc.AppEvents;
import id.co.rho.scoring.cid.client.security.Credential;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.Info;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class TypeModelConfigView extends View {
	private TypeModelConfigUI ui;
	private CommandDispatcherAsync dispatcher;
	private Credential credential;
	private boolean addData = false;
	private long id;

	@Inject
	public TypeModelConfigView(TypeModelConfigUI ui, Credential credential,
			CommandDispatcherAsync dispatcher) {
		super(null);
		this.ui = ui;
		controller = new ViewController();
		this.dispatcher = dispatcher;
		this.credential = credential;
	}

	@Override
	public void initialize() {
		super.initialize();
		ui.initialize();
		ui.getBtnSave().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						saveData();
					}
				});

		ui.getBtnCancel().addSelectionListener(
				new SelectionListener<ButtonEvent>() {

					@Override
					public void componentSelected(ButtonEvent ce) {
						AppEvent appEvent = new AppEvent(
								AppEvents.TypeModelConfigSearchView);
						Dispatcher.forwardEvent(appEvent);
					}
				});
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType().equals(AppEvents.TypeModelConfigView)) {
			if (event.getData("id") != null) {
				id = Long.parseLong(event.getData("id").toString());
				addData = false;
				ui.getTxtCode().setValue(event.getData("code").toString());
				ui.getTxtName().setValue(event.getData("name").toString());
				ui.getTxtDescr().setValue(event.getData("descr").toString());
				ui.show();
			} else {
				addData = true;
				id = 0;
				ui.clearText();
				ui.show();
			}
		}
	}

	private void saveData() {
		TypeModelConfigCommand cmd = new TypeModelConfigCommand();
		if (addData == true) {
			cmd.setCommandType(CommandType.ADD);
			cmd.setCreatedBy(credential.getUsername());
		} else {
			cmd.setUpdatedBy(credential.getUsername());
			cmd.setCommandType(CommandType.EDIT);
			cmd.setId(id);
		}
		cmd.setCode(ui.getTxtCode().getValue());
		cmd.setName(ui.getTxtName().getValue());
		cmd.setDescr(ui.getTxtDescr().getValue());
		
		dispatcher.execute(cmd, new AsyncCallback<TypeModelConfigResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(TypeModelConfigResult result) {
				if (result.isSaved() == initialized) {
					Info.display("Saved", "Data has been saved");
					AppEvent appEvent = new AppEvent(
							AppEvents.TypeModelConfigSearchView);
					Dispatcher.forwardEvent(appEvent);
				}
			}

		});
	}

	private void deleteData() {
		TypeModelConfigCommand cmd = new TypeModelConfigCommand();
		cmd.setCommandType(CommandType.DELETE);
		cmd.setDeletedby(credential.getUsername());
		cmd.setId(id);
		dispatcher.execute(cmd, new AsyncCallback<TypeModelConfigResult>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(TypeModelConfigResult result) {
				if (result.isSaved() == initialized) {
					Info.display("Deleted", "Data has been deleted");
					AppEvent appEvent = new AppEvent(
							AppEvents.TypeModelConfigSearchView);
					Dispatcher.forwardEvent(appEvent);
				}
			}

		});
	}

	private class ViewController extends Controller {
		private ViewController() {
			registerEventTypes(AppEvents.TypeModelConfigView);
		}

		@Override
		public void handleEvent(AppEvent event) {
			forwardToView(TypeModelConfigView.this, event);
		}

	}
}
